#!/usr/bin/env bash

export SCRIPT_PARENT_DIR=$(dirname "${BASH_SOURCE[0]}")/..
# OneX 项目源码根目录
export ONEX_ROOT=${ONEX_ROOT:-${SCRIPT_PARENT_DIR}}

# 生成文件存放目录
export LOCAL_OUTPUT_ROOT="${ONEX_ROOT}/${OUT_DIR:-_output}"

# 设置 OneX 项目统一的用户名/密码，方便记忆
export ONEX_USERNAME=${ONEX_USERNAME:-onex}
export ONEX_PASSWORD=${ONEX_PASSWORD:-'onex(#)666'}

# Linux系统 going 用户
export LINUX_USERNAME=${LINUX_USERNAME:-going}
# Linux root & going 用户密码
export LINUX_PASSWORD=${LINUX_PASSWORD:-${ONEX_PASSWORD}}

# OneX 通用配置
export ONEX_INSTALL_DIR=${ONEX_INSTALL_DIR:-/opt/onex} # onex 安装文件存放目录（基础目录）
export ONEX_DATA_DIR=${ONEX_DATA_DIR:-${ONEX_INSTALL_DIR}/data} # onex 各组件数据目录
export ONEX_BIN_DIR=${ONEX_BIN_DIR:-${ONEX_INSTALL_DIR}/bin} # onex 各组件二进制文件存放目录
export ONEX_CONFIG_DIR=${ONEX_CONFIG_DIR:-${ONEX_INSTALL_DIR}/etc} # onex 配置文件存放目录
export ONEX_LOG_DIR=${ONEX_LOG_DIR:-${ONEX_INSTALL_DIR}/log} # onex 日志文件存放目录
export ONEX_CA_FILE=${ONEX_CA_FILE:-${ONEX_CONFIG_DIR}/cert/ca.pem} # CA
# 为了简化安装，所有组件的绑定地址为 0.0.0.0
# OneX 组件的统一访问 IP 地址，通常需要配置为安装所在的机器 IP 地址
export ONEX_ACCESS_HOST=${ONEX_ACCESS_HOST:-127.0.0.1}
# 为了防止在安装时跟本地已有组件起端口冲突，常用的第三方组件端口可以加一个端口前缀号
# 增加了部署的复杂度，但为了能让部署成功率更高，就加了这么一个端口号前缀
# 有些不常用的组件不需要加这个端口号前缀
export ONEX_ACCESS_PORT_PREFIX=${ONEX_ACCESS_PORT_PREFIX:-""}

# MariaDB 配置信息
export ONEX_MYSQL_PORT=${ONEX_MYSQL_PORT:-${ONEX_ACCESS_PORT_PREFIX}3306}
export ONEX_MYSQL_HOST=${ONEX_ACCESS_HOST:-127.0.0.1} # MariaDB 主机地址
export ONEX_MYSQL_ADMIN_USERNAME=${ONEX_MYSQL_ADMIN_USERNAME:-root} # MariaDB root 用户
export ONEX_MYSQL_ADMIN_PASSWORD=${ONEX_MYSQL_ADMIN_PASSWORD:-${ONEX_PASSWORD}} # MariaDB root 用户密码
export ONEX_MYSQL_DATABASE=${ONEX_MYSQL_DATABASE:-onex} # MariaDB onex 应用使用的数据库名
export ONEX_MYSQL_USERNAME=${ONEX_MYSQL_USERNAME:-onex} # onex 数据库用户名
export ONEX_MYSQL_PASSWORD=${ONEX_MYSQL_PASSWORD:-${ONEX_PASSWORD}} # onex 数据库用户名
export ONEX_MYSQL_LOG_LEVEL=${ONEX_MYSQL_LOG_LEVEL:-1} # 数据库日志级别，1 为最低，4 为最高

# Redis 配置信息
export ONEX_REDIS_PORT=${ONEX_REDIS_PORT:-${ONEX_ACCESS_PORT_PREFIX}6379}
export ONEX_REDIS_HOST=${ONEX_ACCESS_HOST:-127.0.0.1} # Redis 主机地址
export ONEX_REDIS_ADDR=${ONEX_REDIS_ADDR:-${ONEX_REDIS_HOST}:${ONEX_REDIS_PORT}}
export ONEX_REDIS_DATABASE=${ONEX_REDIS_DATABASE:-0} # Redis 主机地址
export ONEX_REDIS_USERNAME=${ONEX_REDIS_USERNAME:-"<none>"} # Redis 用户名
export ONEX_REDIS_PASSWORD=${ONEX_REDIS_PASSWORD:-${ONEX_PASSWORD}} # Redis 密码

# Etcd 配置信息
export ONEX_ETCD_HOST=${ONEX_ACCESS_HOST:-127.0.0.1}
export ONEX_ETCD_CLIENT_PORT=${ONEX_ETCD_CLIENT_PORT:-${ONEX_ACCESS_PORT_PREFIX}2379}
export ONEX_ETCD_PEER_PORT=${ONEX_ETCD_PEER_PORT:-${ONEX_ACCESS_PORT_PREFIX}2380}
# Etcd 端点列表
export ONEX_ETCD_ENDPOINTS=${ONEX_ETCD_ENDPOINTS:-${ONEX_ETCD_HOST}:${ONEX_ETCD_CLIENT_PORT}}

# MongoDB 配置
export ONEX_MONGO_HOST=${ONEX_ACCESS_HOST:-127.0.0.1} # MongoDB 地址
export ONEX_MONGO_PORT=${ONEX_MONGO_PORT:-27017} # MongoDB 端口
export ONEX_MONGO_URL=${ONEX_MONGO_URL:-${ONEX_MONGO_HOST}:${ONEX_MONGO_PORT}} # MongoDB URL
export ONEX_MONGO_ADMIN_USERNAME=${ONEX_MONGO_ADMIN_USERNAME:-root} # MongoDB root 用户
export ONEX_MONGO_ADMIN_PASSWORD=${ONEX_MONGO_ADMIN_PASSWORD:-${ONEX_PASSWORD}} # MongoDB root 用户密码
export ONEX_MONGO_DATABASE=${ONEX_MONGO_DATABASE:-onex} # MongoDB 用户名
export ONEX_MONGO_COLLECTION=${ONEX_MONGO_COLLECTION:-audit} # MongoDB 密码
export ONEX_MONGO_USERNAME=${ONEX_MONGO_USERNAME:-onex} # MongoDB 用户名
export ONEX_MONGO_PASSWORD=${ONEX_MONGO_PASSWORD:-${ONEX_PASSWORD}} # MongoDB 密码
export ONEX_MONGO_TIMEOUT=${ONEX_MONGO_TIMEOUT:-30s} # MongoDB 密码

# Kafka 配置
export ONEX_KAFKA_HOST=${ONEX_ACCESS_HOST:-127.0.0.1}
export ONEX_KAFKA_PORT=${ONEX_KAFKA_PORT:-${ONEX_ACCESS_PORT_PREFIX}9092}
export ONEX_KAFKA_BROKERS=${ONEX_KAFKA_BROKERS:-${ONEX_KAFKA_HOST}:${ONEX_KAFKA_PORT}}
export ONEX_KAFKA_TOPIC=${ONEX_KAFKA_TOPIC:-audit}
export ONEX_KAFKA_TIMEOUT=${ONEX_KAFKA_TIMEOUT:-3s}

# Jaeger 配置
export ONEX_JAEGER_HOST=${ONEX_ACCESS_HOST:-127.0.0.1}
export ONEX_JAEGER_PORT=${ONEX_JAEGER_PORT:-${ONEX_ACCESS_PORT_PREFIX}4317}
export ONEX_JAEGER_ENV=${ONEX_JAEGER_ENV:-dev}
export ONEX_JAEGER_ENDPOINT=${ONEX_JAEGER_ENDPOINT:-${ONEX_JAEGER_HOST}:${ONEX_JAEGER_PORT}}

# Kubeconfig 路径配置
export ONEX_ADMIN_KUBECONFIG=${ONEX_ADMIN_KUBECONFIG:-${ONEX_CONFIG_DIR}/config}

# OneX 服务配置
#
## onex-fakeserver 配置
export ONEX_FAKESERVER_HTTP_PORT=${ONEX_FAKESERVER_HTTP_PORT:-58843}
export ONEX_FAKESERVER_GRPC_PORT=${ONEX_FAKESERVER_GRPC_PORT:-58090}
export ONEX_FAKESERVER_HTTP_ADDR=${ONEX_FAKESERVER_HTTP_ADDR:-0.0.0.0:${ONEX_FAKESERVER_HTTP_PORT}}
export ONEX_FAKESERVER_GRPC_ADDR=${ONEX_FAKESERVER_GRPC_ADDR:-0.0.0.0:${ONEX_FAKESERVER_GRPC_PORT}}
export ONEX_FAKESERVER_TLS_USE_TLS=${ONEX_FAKESERVER_TLS_USE_TLS:-false}
export ONEX_FAKESERVER_TLS_CERT=${ONEX_FAKESERVER_TLS_CERT:-${ONEX_CONFIG_DIR}/cert/onex-fakeserver.pem}
export ONEX_FAKESERVER_TLS_KEY=${ONEX_FAKESERVER_TLS_KEY:=${ONEX_CONFIG_DIR}/cert/onex-fakeserver-key.pem}

## onex-usercenter 配置
export ONEX_USERCENTER_HTTP_PORT=${ONEX_USERCENTER_HTTP_PORT:-50843}
export ONEX_USERCENTER_GRPC_PORT=${ONEX_USERCENTER_GRPC_PORT:-50090}
export ONEX_USERCENTER_HTTP_ADDR=${ONEX_USERCENTER_HTTP_ADDR:-0.0.0.0:${ONEX_USERCENTER_HTTP_PORT}}
export ONEX_USERCENTER_GRPC_ADDR=${ONEX_USERCENTER_GRPC_ADDR:-0.0.0.0:${ONEX_USERCENTER_GRPC_PORT}}
export ONEX_USERCENTER_TLS_USE_TLS=${ONEX_USERCENTER_TLS_USE_TLS:-false}
export ONEX_USERCENTER_TLS_CERT=${ONEX_USERCENTER_TLS_CERT:-${ONEX_CONFIG_DIR}/cert/onex-usercenter.pem}
export ONEX_USERCENTER_TLS_KEY=${ONEX_USERCENTER_TLS_KEY:=${ONEX_CONFIG_DIR}/cert/onex-usercenter-key.pem}
export ONEX_USERCENTER_REDIS_DATABASE=${ONEX_USERCENTER_REDIS_DATABASE:=${ONEX_REDIS_DATABASE}}

## onex-apiserver
export ONEX_APISERVER_SECURE_PORT=${ONEX_APISERVER_SECURE_PORT:-52443}
export ONEX_APISERVER_BIND_ADDRESS=${ONEX_APISERVER_BIND_ADDRESS:-0.0.0.0}
export ONEX_APISERVER_CLIENT_CA_FILE=${ONEX_APISERVER_CLIENT_CA_FILE:-${ONEX_CA_FILE}}
export ONEX_APISERVER_TLS_CERT_FILE=${ONEX_APISERVER_TLS_CERT_FILE:-${ONEX_CONFIG_DIR}/cert/onex-apiserver.pem}
export ONEX_APISERVER_TLS_PRIVATE_KEY_FILE=${ONEX_APISERVER_TLS_PRIVATE_KEY_FILE:-${ONEX_CONFIG_DIR}/cert/onex-apiserver-key.pem}
export ONEX_APISERVER_ETCD_SERVERS=${ONEX_APISERVER_ETCD_SERVERS:-${ONEX_ETCD_ENDPOINTS}}
export ONEX_APISERVER_V_LEVEL=${ONEX_APISERVER_V_LEVEL:-7}

## onex-gateway 配置
export ONEX_GATEWAY_HTTP_PORT=${ONEX_GATEWAY_HTTP_PORT:-51843}
export ONEX_GATEWAY_GRPC_PORT=${ONEX_GATEWAY_GRPC_PORT:-51090}
export ONEX_GATEWAY_HTTP_ADDR=${ONEX_GATEWAY_HTTP_ADDR:-0.0.0.0:${ONEX_GATEWAY_HTTP_PORT}}
export ONEX_GATEWAY_GRPC_ADDR=${ONEX_GATEWAY_GRPC_ADDR:-0.0.0.0:${ONEX_GATEWAY_GRPC_PORT}}
export ONEX_GATEWAY_TLS_USE_TLS=${ONEX_GATEWAY_TLS_USE_TLS:-false}
export ONEX_GATEWAY_TLS_CERT=${ONEX_GATEWAY_TLS_CERT:-${ONEX_CONFIG_DIR}/cert/onex-usercenter.pem}
export ONEX_GATEWAY_TLS_KEY=${ONEX_GATEWAY_TLS_KEY:=${ONEX_CONFIG_DIR}/cert/onex-usercenter-key.pem}
export ONEX_GATEWAY_REDIS_DATABASE=${ONEX_GATEWAY_REDIS_DATABASE:=${ONEX_REDIS_DATABASE}}
export ONEX_GATEWAY_USERCENTER_SERVER=${ONEX_GATEWAY_USERCENTER_SERVER:-${ONEX_ACCESS_HOST}:${ONEX_USERCENTER_GRPC_ADDR}}

# onex-nightwatch
export ONEX_NIGHTWATCH_HTTP_PORT=${ONEX_NIGHTWATCH_HTTP_PORT:-54082}
export ONEX_NIGHTWATCH_HEALTH_ENABLE_HTTP_PROFILE=${ONEX_NIGHTWATCH_HEALTH_ENABLE_HTTP_PROFILE:-true}
export ONEX_NIGHTWATCH_HEALTH_CHECK_PATH=${ONEX_NIGHTWATCH_HEALTH_CHECK_PATH:-/healthz}
export ONEX_NIGHTWATCH_HEALTH_CHECK_ADDRESS=${ONEX_NIGHTWATCH_HEALTH_CHECK_ADDRESS:-0.0.0.0:${ONEX_NIGHTWATCH_HTTP_PORT}}
export ONEX_NIGHTWATCH_REDIS_DATABASE=${ONEX_NIGHTWATCH_REDIS_DATABASE:-${ONEX_REDIS_DATABASE}}

## onex-pump 配置
export ONEX_PUMP_HTTP_PORT=${ONEX_PUMP_HTTP_PORT:-55082}
export ONEX_PUMP_HEALTH_ENABLE_HTTP_PROFILER=${ONEX_PUMP_HEALTH_ENABLE_HTTP_PROFILER:-true}
export ONEX_PUMP_HEALTH_CHECK_PATH=${ONEX_PUMP_HEALTH_CHECK_PATH:-/healthz}
export ONEX_PUMP_HEALTH_CHECK_ADDRESS=${ONEX_PUMP_HEALTH_CHECK_ADDRESS:-0.0.0.0:${ONEX_PUMP_HTTP_PORT}}
export ONEX_PUMP_REDIS_DATABASE=${ONEX_PUMP_REDIS_DATABASE:-${ONEX_REDIS_DATABASE}}

## onex-controller-mananger

## onex-miner-controller
export ONEX_MINER_CONTROLLER_REDIS_DATABASE=${ONEX_MINER_CONTROLLER_REDIS_DATABASE:-${ONEX_REDIS_DATABASE}}

## onex-minerset-controller

## onex-toyblc
export ONEX_TOYBLC_ADDRESS=${ONEX_TOYBLC_ADDRESS:-0x210d9eD12CEA87E33a98AA7Bcb4359eABA9e800e}
export ONEX_TOYBLC_MINER=${ONEX_TOYBLC_MINER:-false}
export ONEX_TOYBLC_P2P_ADDR=${ONEX_TOYBLC_P2P_ADDR:-0.0.0.0:6001}
export ONEX_TOYBLC_PEERS=${ONEX_TOYBLC_PERRS:-ws://localhost:6001}
export ONEX_TOYBLC_HTTP_ADDR=${ONEX_TOYBLC_HTTP_ADDR:-0.0.0.0:56080}

# onex-cacheserver
export ONEX_CACHESERVER_GRPC_PORT=${ONEX_CACHESERVER_GRPC_PORT:-57090}
export ONEX_CACHESERVER_DISABLE=${ONEX_CACHESERVER_DISABLE:-true}
export ONEX_CACHESERVER_GRPC_ADDR=${ONEX_CACHESERVER_GRPC_ADDR:-0.0.0.0:${ONEX_CACHESERVER_GRPC_PORT}}
export ONEX_CACHESERVER_TLS_USE_TLS=${ONEX_CACHESERVER_TLS_USE_TLS:-false}
export ONEX_CACHESERVER_TLS_CERT=${ONEX_CACHESERVER_TLS_CERT:-${ONEX_CONFIG_DIR}/cert/onex-cacheserver.pem}
export ONEX_CACHESERVER_TLS_KEY=${ONEX_CACHESERVER_TLS_KEY:-${ONEX_CONFIG_DIR}/cert/onex-cacheserver-key.pem}
export ONEX_CACHESERVER_REDIS_DATABASE=${ONEX_CACHESERVER_REDIS_DATABASE:-${ONEX_REDIS_DATABASE}}

# onexctl
export ONEXCTL_USER_USERNAME=${ONEXCTL_USER_USERNAME:-admin}
export ONEXCTL_USER_PASSWORD=${ONEXCTL_USER_PASSWORD:-${ONEX_PASSWORD}}
export ONEXCTL_USERCENTER_ADDR=${ONEXCTL_USERCENTER_ADDR:-${ONEX_ACCESS_HOST}:${ONEX_USERCENTER_GRPC_PORT}}
export ONEXCTL_GATEWAY_ADDR=${ONEXCTL_GATEWAY_ADDR:-${ONEX_ACCESS_HOST}:${ONEX_GATEWAY_GRPC_PORT}}
